/**
 * Created whit IntelliJ IDEA.
 * Description：
 * User：ZHONGCHEN
 * Date:2022-03-18
 * Time:10:45
 */

import java.util.Scanner;

/**
 * 2043. 简易银行系统
 * 实现 Bank 类：
 * Bank(long[] balance) 使用下标从 0 开始的整数数组 balance 初始化该对象。
 * boolean transfer(int account1, int account2, long money) 从编号为 account1 的账户向编号为 account2 的账户转帐 money 美元。如果交易成功，返回 true ，否则，返回 false 。
 * boolean deposit(int account, long money) 向编号为 account 的账户存款 money 美元。如果交易成功，返回 true ；否则，返回 false 。
 * boolean withdraw(int account, long money) 从编号为 account 的账户取款 money 美元。如果交易成功，返回 true ；否则，返回 false 。
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/simple-bank-system
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class Bank {
    long[] bank;

    public Bank(long[] balance) {
        this.bank = balance;
    }

    private boolean check(int account) {
        if (account < 1 || account > bank.length) {
            return false;
        }
        return true;
    }

    public boolean transfer(int account1, int account2, long money) {
        if (check(account1) && check(account2)) {
            if (bank[account1 - 1] >= money) {
                bank[account1 - 1] -= money;
                bank[account2 - 1] += money;
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    public boolean deposit(int account, long money) {
        if (check(account)) {
            bank[account - 1] += money;
            return true;
        }
        return false;
    }

    public boolean withdraw(int account, long money) {
        if (check(account)) {
            if (bank[account - 1] >= money) {
                bank[account - 1] -= money;
                return true;
            }
        }
        return false;
    }

    //牛客-年终奖-递归写法
    static int max = 0;

    public int getMost(int[][] board) {
        getNumMax(board, 0, 0, 0);
        int ans = max;
        max = 0;
        return ans;
    }

    private void getNumMax(int[][] board, int row, int col, int sum) {
        if (row == board.length - 1 && col == board[0].length - 1) {
            if (max < sum) {
                sum += board[row][col];
                max = sum;
            }
            return;
        }
        if (row == board.length - 1 && col != board[0].length - 1) {
            sum += board[row][col];
            getNumMax(board, row, col + 1, sum);
            return;
        }
        if (row != board.length - 1 && col == board[0].length - 1) {
            sum += board[row][col];
            getNumMax(board, row + 1, col, sum);
            return;
        }
        sum += board[row][col];
        getNumMax(board, row + 1, col, sum);
        getNumMax(board, row, col + 1, sum);
    }

    //牛客-迷宫问题-回溯算法
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int[][] num = new int[n][m];
        int[][] ans = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                num[i][j] = scanner.nextInt();
                ans[i][j] = 0;
            }
        }
        findWay(num, ans, 0, 0);
    }

    private static void findWay(int[][] num, int[][] ans, int row, int col) {
        if (row == num.length - 1 && col == num[0].length - 1) {
            ans[row][col] = 1;
            print(ans,0,0);
            return;
        }
        if (row < 0 || row >= num.length || col < 0 || col >= num[0].length || num[row][col] == 1) {
            return;
        }
        ans[row][col] = 1;
        num[row][col] = 1;
        findWay(num, ans, row + 1, col);
        findWay(num, ans, row - 1, col);
        findWay(num, ans, row, col + 1);
        findWay(num, ans, row, col - 1);
        num[row][col] = 1;
        ans[row][col] = 0;
    }

    private static void print(int[][] ans, int i, int j) {
        if (i == -1 || i == ans.length || j == -1 || j == ans[0].length || ans[i][j] == 0) {
            return;
        }
        System.out.println("(" + i + "," + j + ")");
        ans[i][j] = 0;
        print(ans, i + 1, j);
        print(ans, i - 1, j);
        print(ans, i, j + 1);
        print(ans, i, j - 1);
        ans[i][j] = 1;
    }
}
